package main.java.com.fronted.css;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.plaf.basic.BasicButtonUI;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;

public class New_botton {
    static class RoundedButtonUI extends BasicButtonUI {
        @Override
        protected void installDefaults(AbstractButton button) {
            super.installDefaults(button);
            button.setOpaque(false);
        }

        protected void paintBackground(Graphics g, AbstractButton button) {
            Graphics2D g2 = (Graphics2D) g.create();
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setColor(button.getBackground());
            g2.fill(new RoundRectangle2D.Float(0, 0, button.getWidth(), button.getHeight(), 20, 20));
            g2.dispose();
        }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Rounded Button Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 600);

        JButton roundedButton = new JButton("Click me");
        roundedButton.setPreferredSize(new Dimension(100, 40));
        roundedButton.setForeground(Color.WHITE);
        roundedButton.setBackground(Color.BLUE);
        roundedButton.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        roundedButton.setFocusPainted(false);
        roundedButton.setContentAreaFilled(false);
        roundedButton.setOpaque(true);
        roundedButton.setUI(new RoundedButtonUI());
        roundedButton.setLayout(null);

        frame.getContentPane().add(roundedButton);
        frame.setVisible(true);
        //专门设置类，只用于复制上述按钮的UI风格

        try {
            BufferedImage originalImage = ImageIO.read(new File("src/main/resources/page1.png"));
            int borderWidth = 8; // 边框宽度
            int width = 200 + 2 * borderWidth;
            int height = 200 + 2 * borderWidth;
            BufferedImage borderedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics2D g2d = borderedImage.createGraphics();
            g2d.setColor(Color.green);
            g2d.fillRect(150, 50, width, height);
            g2d.drawImage(originalImage, borderWidth, borderWidth, null);
            // 释放资源
            g2d.dispose();
            JLabel imageLabel = new JLabel(new ImageIcon(borderedImage));
            // 将标签添加到窗口中
            frame.getContentPane().add(imageLabel);
        }catch (Exception e) {
            e.printStackTrace();
        }
        frame.pack();
        frame.setVisible(true);
    }
}
